import React from 'react'
import { StackActions } from 'react-navigation'
import BaseUserAvator from 'src/pageP/base/base-user-avator'
import { uploadImage } from 'src/utils/RequestAPI'
import { CommonToast, DismissLoading, ShowLoading } from 'src/utils/ToastUtil'

import { connect } from 'react-redux'
import { DO_REQUEST_SAVEUSERINFO_B_ACTION } from '@store/actions/userinfo'

const mapDispatchToProps = dispatch => ({
  updateUserInfoB(payload) {
    dispatch(DO_REQUEST_SAVEUSERINFO_B_ACTION(payload))
  }
})
@connect(
  ({ userinfo }) => ({
    userinfo
  }),
  mapDispatchToProps
)

class CompanyAvator extends BaseUserAvator {
  constructor(props) {
    super(props)
    const {
      navigation: {
        state: { params: { userName, userAvator } }
      }
    } = this.props
    this.state = {
      ...this.state,
      userName,
      userAvator
    }
  }

  /**
   * 下一步
   * @private
   */
  nextButtonClick = () => {
    const { userAvator: uri } = this.state
    if (!uri || !uri.startsWith('file:')) {
      CommonToast('您还未选择新的图像')
      return
    }
    ShowLoading()
    uploadImage({ params: { uri, type: 'company', mime: this.state.mime } }).then(res => {
      const userAvator = res.data
      const {
        navigation: {
          state: { params: { getData } }
        }
      } = this.props
      const data = { companyLogo: userAvator }
      getData(data, data, () => {
        CommonToast('保存成功')
      }, '', true)
    }).finally(() => {
      DismissLoading()
      this.goBack()
    })
  }
}
export default CompanyAvator
